<?php
namespace Index\Controller;
use Think\Controller;
header("Content-type: text/html; charset=utf-8"); 
class AptController extends Controller {

    public function index(){
    	$key=trim(I("key"));
    	$cid=I("cid");
    	$aid=I("aid");
    	
		
		
    	$options=C('options');   		 
    	$weObj = new \Org\Util\Wechat($options);
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
		$city=M('city')->select();
		if($cid!=""){
		$cname=M('city')->where('id='.$cid)->find();
		}
		if($key!=""){
			$where['name']= array('like','%'.$key.'%');
			
		}
		if($cid!=""){
			$where['cid']= $cid;
		}
		if($aid!=""){
			$where['aid']= $aid;
		}
		$apt=M('apt')->where($where)->select();
		
		
		
		
		$preg ="/<img src=\"(.+?)\".*?>/";//只能有src	
		foreach ($apt as $key => $value) {
			preg_match_all($preg,html_entity_decode($value['listimg']),$listimg);		
			$listimg_arr=array_unique($listimg[1]);//去除数组中重复的值			
			$value['src']=$listimg_arr[0];
			$a[]=$value;
			}	
		$apt=$a;
        $sign = $weObj->getJsSign($url);	
		
		$barder=M('listimg')->select();
		foreach ($barder as $k => $v) {
			preg_match_all($preg,html_entity_decode($v['img']),$img);		
			$img_arr=array_unique($img[1]);//去除数组中重复的值			
			$v['src']=$img_arr[0];
			$b[]=$v;
			}	
		$barder=$b;
		$this->assign('ms',$barder);
		$this->assign('cname',$cname['city']);
		$this->assign('cid',$cid);
		$this->assign('id',$barder);
 		$this->assign('barder',$barder);
        $this->assign('arr',$sign);
		$this->assign('city',$city);
		$this->assign('apt',$apt);
        $this->display();
	}

    public function show(){
    	$id=I('id');	
		$apt=M('apt')->where("id=".$id)->find();
		$preg ="/<img src=\"(.+?)\".*?>/";//只能有src	
		preg_match_all($preg,html_entity_decode($apt['kvimg']),$listimg);		
		$listimg_arr=$listimg[1];//去除数组中重复的值		
		preg_match_all($preg,html_entity_decode($apt['motimg']),$motimg);		
		$motimg_src=$motimg[1][0];//去除数组中重复的值	
		preg_match_all($preg,html_entity_decode($apt['psimg']),$psimg);		
		$psimg_arr=$psimg[1];//去除数组中重复的值		
				
		$hx=M('fangxing')->where("hid=".$id)->select();
			foreach ($hx as $key => $value) {
			preg_match_all($preg,html_entity_decode($value['img']),$img);		
			$img_arr=array_unique($img[1]);//去除数组中重复的值			
			$value['src']=$img_arr[0];
			$a[]=$value;
			}	
		$hx=$a;
		$fw=M('fw')->select();	
		
		$afw=explode(",", $apt['fw']);
		
		$this->assign('fw1',$fw);
		$this->assign('fw',$fw);
		$this->assign('afw',$afw);
		$this->assign('afw1',$afw);
		$this->assign('hx',$hx);
		$this->assign('mot',$motimg_src);
		$this->assign('ps',$psimg_arr);
		$this->assign('bn',$listimg_arr);
		$this->assign('apt',$apt);
        $this->display();
	}

  public function gps(){
  		
    	$apt=M('apt')->field('map')->select();
		$this->assign('map',json_encode($apt));
		
        $this->display();
	}

	public function cityid(){
		$name=I("name");
		$where['name']= array('like','%'.$name.'%');
		$city=M('city')->where($where)->find();
		if($city){
		$this->ajaxReturn(array('s'=>"true",'m'=>$city['id']));
		}
	}
	
	public function gpslist(){
		$id=I("id");
		$where['cid']=$id;
		$apt=M('apt')->where($where)->select();
		
		if($apt){
			$preg ="/<img src=\"(.+?)\".*?>/";//只能有src	
			foreach ($apt as $key => $value) {
			preg_match_all($preg,html_entity_decode($value['listimg']),$listimg);		
			$listimg_arr=array_unique($listimg[1]);//去除数组中重复的值			
			$value['src']=$listimg_arr[0];
			$a[]=$value;
			}	
			$apt=$a;
		
			$this->ajaxReturn(array('s'=>"true",'m'=>$apt));
		}else{
			$this->ajaxReturn(array('s'=>"false"));
		}
		
	
	}
	public function cas(){
		$cid=I('cid');
		
		$city=M('city')->select();
		if($cid!=""){
		$cname=M('city')->where('id='.$cid)->find();
		}
		$this->assign('cname',$cname['city']);
		$this->assign('city',$city);
		$this->assign('cid',$cid);
		$this->display();
	
	}
	public function casarea(){
		$cid=I('cid');
		$area=M('area')->where('cid='.$cid)->select();
		if($area){
			$this->ajaxReturn(array('s'=>"true",'m'=>$area));
		}else{
			$this->ajaxReturn(array('s'=>"false"));
		}
	
	}


 
  
}
?>